package com.nsu.evaluation.service.impl;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.dao.RoleMapper;
import com.nsu.evaluation.dao.RoleUserMapper;
import com.nsu.evaluation.pojo.Role;
import com.nsu.evaluation.pojo.RoleUser;
import com.nsu.evaluation.pojo.RoleUserExample;
import com.nsu.evaluation.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleUserServiceImpl implements RoleUserService {

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Autowired
    private RoleServiceImpl roleServiceImpl;

    /**
     * 根据用户Id获取角色名
     * @param userId
     * @return
     */
    @Override
    public ServerResponse<List<Role>> getRoleNameByUserId(String userId){
        //根据用户名获取到RoleUser列表
        RoleUserExample roleUserExample = new RoleUserExample();
        RoleUserExample.Criteria criteria = roleUserExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<RoleUser> roleUserList = roleUserMapper.selectByExample(roleUserExample);
        if(null != roleUserList && roleUserList.size()>0){
            //从列表中获取roleId，并根据roleId查询到roleName
            RoleUser roleUser = roleUserList.get(0);
            Role role = new Role();
            role.setId(roleUser.getRoleId());
            List<Role> roleList = roleServiceImpl.selectRoleByExample(role).getData();
            if(null != roleList && roleList.size()>0){
                return ServerResponse.createBySuccess("获取角色名成功",roleList);
            }
        }
        return ServerResponse.createByErrorMessage("获取角色名失败");
    }

    /**
     * 新增角色-用户信息
     * @param roleUser
     * @return
     */
    @Override
    public ServerResponse addRoleUser(RoleUser roleUser){
        int rowCount = roleUserMapper.insert(roleUser);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("新增角色-用户成功");
        }
        return ServerResponse.createByErrorMessage("新增角色-用户失败");
    }
}
